#include "CCellAnimation.h"
#include "SGD Wrappers\CSGD_TextureManager.h"

void CCellAnimation::Update(float _fDT)
{
	if( !m_bIsPlaying)
		return;

	m_fTimeWaited += _fDT*m_fSpeed;
	if( m_fTimeWaited > m_vFrames[m_nCurrFrame].fDuration)
	{
		m_fTimeWaited -= m_vFrames[m_nCurrFrame].fDuration;
		m_nCurrFrame++;
	
		if(m_nCurrFrame >= m_vFrames.size())
		{
			if(m_bIsLooping)
			Reset();	
			else
			{
				Stop();
				m_nCurrFrame = m_vFrames.size() -1;
			}	
		}
	
	}
}

void CCellAnimation::Render(int _nPosX, int _nPosY, bool _bIsXFlipped, float _fScale, DWORD _dwColor)
{
	RECT rFrame = m_vFrames[m_nCurrFrame].rFrame;
	POINT ptAnchor = m_vFrames[m_nCurrFrame].pAnchor;

	float fScaleX = _fScale;

	if( _bIsXFlipped)
	{
		fScaleX = -1.0f;
		int width = rFrame.right - rFrame.left;
		_nPosX += width * _fScale;
		ptAnchor.x = width - ptAnchor.x;
	}

	CSGD_TextureManager::GetInstance()->Draw(m_nImageID, _nPosX - ptAnchor.x* _fScale, _nPosY - ptAnchor.y * _fScale, fScaleX, _fScale, &rFrame, 0,0,0 &_dwColor);
}
